//
//  ViewController.m
//  TabelibleScrollerViewSample
//
//  Created by chenxinju on 16/6/19.
//  Copyright © 2016年 Innovation. All rights reserved.
//

#import "XJTabelibleScrollerViewController.h"
#import "XJFlexibleParallaxView.h"
#import "XJUserInfoView.h"
#import "XJButtonGroup.h"

@interface XJTabelibleScrollerViewController ()<XJFlexibleParallaxViewDelegate>

//按钮的数组
@property (nonatomic, strong) XJButtonGroup *buttonGroup;
// scrollview
@property (nonatomic, strong) XJFlexibleParallaxView *FlexibleView;

@property (nonatomic, assign) CGRect defaultRect;


@end

@implementation XJTabelibleScrollerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置透明
    [self.navigationController.navigationBar fs_setBackgroundColor:[UIColor clearColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //返回
    [self prepareComposeVc];
    
    
  /**
    [self.FlexibleView addSubView:self.topView];
    [self.FlexibleView addSubView:self.userView];
   
    //    topView.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.FlexibleView.mas_top);
        make.left.equalTo(self.FlexibleView.mas_left);
        make.width.equalTo(self.FlexibleView.mas_width);
        make.height.mas_equalTo(200);
    }];
    
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.FlexibleView.mas_left);
        make.right.equalTo(self.FlexibleView.mas_right);
        make.top.equalTo(self.topView.mas_bottom);
        make.height.mas_equalTo(60);
    }];
    
    */
    
        //视图展示
        UIImageView *topView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
        topView.frame  = CGRectMake(0, 0, self.view.frame.size.width, 200);
    
    
    
        XJUserInfoView *userView = [[XJUserInfoView alloc] init];
        userView.frame = CGRectMake(0, CGRectGetMaxY(topView.frame), self.view.width, 60);

    
    XJButtonGroup  *buttons = [[XJButtonGroup alloc]initWithButtons:@[@"composer_rating_icon_highlighted",@"preview_like_icon_highlighted",@"compose_photo_preview_right"]];
    buttons.frame = CGRectMake(200, topView.height + userView.height -50, 200, 44);
    self.defaultRect = buttons.frame;
    self.buttonGroup = buttons;
    
    
    
    XJFlexibleParallaxView *flexibleView = [[XJFlexibleParallaxView alloc]initWithFrame:self.view.frame topView:topView];
    flexibleView.delegate = self;
    [self.view addSubview:flexibleView];
    
    UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
     imageV.frame = CGRectMake(20, CGRectGetMaxY(userView.frame), self.view.frame.size.width-40, 200);
    imageV.layer.cornerRadius = 10;
    imageV.layer.masksToBounds = YES;

    [flexibleView addSubView:userView];
    [flexibleView addSubView:buttons];
    [flexibleView addSubView:imageV];
    
     [flexibleView setContentSize:CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(imageV.frame)*2.5)];
    
    self.FlexibleView = flexibleView;
}


- (void)prepareComposeVc {

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];


}

//返回
- (void)back {

 [self dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark -  XJFlexibleParallaxViewDelegate

//上拉到导航栏位置 ..把按钮的数组添加
- (void)flexibleParallaxViewDidScrollToTop:(XJFlexibleParallaxView *)flexibleView {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.buttonGroup];
}



- (void)flexibleParallaxViewDidScrollFRomTop:(XJFlexibleParallaxView *)flexibleView {
    self.buttonGroup.frame = _defaultRect;
    [self.FlexibleView addSubView:self.buttonGroup];
}

//改变导航栏的颜色
- (void)flexibleParallaxViewDidScroll:(XJFlexibleParallaxView *)flexibleVie{
    
    CGFloat offsetY = flexibleVie.contentOffset.y;
    //NSLog(@"%f",offsetY);
    
    if (offsetY > 0) {
        CGFloat alpha = (offsetY - 64)/ 64;
        alpha = MIN(alpha, 0.9);
        [self.navigationController.navigationBar fs_setBackgroundColor:[RGBCOLOR(92,80,36) colorWithAlphaComponent:alpha ]];
        
    }else {
     [self.navigationController.navigationBar fs_setBackgroundColor:[UIColor clearColor]];
    
    }
    
}





@end
